import {ref} from 'vue';
import {SparePartApplyListItem, GetSparePartApplyListParam} from '../types';
import apiParts from '@/util/network_api/parts.js'

/**
 * 配件申请单列表服务
 */
export default function () {
	const total = ref(0);
	const sparePartApplyList = ref<Array<SparePartApplyListItem>>([]);
	const getSparePartApplyListParam = ref<GetSparePartApplyListParam>({
		code: '',
		limit: 10,
		networkCode: '',
		networkName: '',
		organizeId: '',
		page: 1,
		status: '',
	});
	
	const getSparePartApplyList = async (isAppend = true) => {
		try {
			const res = await apiParts.getSparePartApplyList(getSparePartApplyListParam.value)
			if (res.code === 20000) {
				total.value = Number(res.data.total)
				if (isAppend) {
					sparePartApplyList.value = [...sparePartApplyList.value, ...res.data.rows]
				} else {
					sparePartApplyList.value = res.data.rows
				}
				return ''
			}
			
			return res.message
		} catch(e) {
			return '获取配件申请单列表失败'
		}
	}
	
	const resetGetSparePartApplyListParam = () => {
		getSparePartApplyListParam.value = {
			code: '',
			limit: 10,
			networkCode: '',
			networkName: '',
			organizeId: '',
			page: 1,
			status: '',
		}
	}

	return {
		total,
		sparePartApplyList,
		getSparePartApplyListParam,
		getSparePartApplyList,
		resetGetSparePartApplyListParam,
	};
}
